//
//  StatusOriginalView.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/14.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import FFLabel
class StatusOriginalView: UIView ,FFLabelDelegate{
    
    //记录原创微博底部约束
    var oringinalBottomConstrains: Constraint?
    
    //数据模型
    var status: Status? {
        didSet{
            iconIMG.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            verifiedIconView.image = status?.user?.verified_type_IMG
            mbankView.image = status?.user?.mbrankIMG
            timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
            sourceLabel.text = status?.source?.linhref().clinetName
            contentLabel.attributedText = EmotionManager.emoticonManager.textToImageText(status?.text ?? "", font: contentLabel.font)
//            //给配图视图设置数据源
//            pictureView.imageURLs = status?.imageURLs
            //移除原创微博底部约束
            self.oringinalBottomConstrains?.uninstall()
            if let picturesURL = status?.imageURLs where picturesURL.count > 0 {
                pictureView.imageURLs = status?.imageURLs
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.oringinalBottomConstrains = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
            }else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.oringinalBottomConstrains = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentLabel.labelDelegate = self
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()  {
//        backgroundColor = UIColor.lightGrayColor()
        //添加子控件视图
        seperatorView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(seperatorView)
        addSubview(iconIMG)
        addSubview(nameLabel)
        addSubview(verifiedIconView)
        addSubview(mbankView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        seperatorView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(statusCellMargin)
        }
        
        iconIMG.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(seperatorView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(snp_left).offset(statusCellMargin)
            make.width.equalTo(statusCellIconWidth)
            make.height.equalTo(statusCellIconWidth)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconIMG.snp_top)
            make.left.equalTo(iconIMG.snp_right).offset(statusCellMargin)
        }
        mbankView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
        }
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconIMG.snp_right)
            make.centerY.equalTo(iconIMG.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconIMG.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(iconIMG.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconIMG.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconIMG.snp_left)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
           self.oringinalBottomConstrains = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
 
    }
    
    
    //懒加载控件
    //用户头像
    private lazy var iconIMG: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户昵称
    private lazy var nameLabel: UILabel = UILabel(title: "花无缺", size: 14, color: UIColor.darkGrayColor())
    //用户认证类型
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var mbankView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: 12, color: UIColor.redColor())
    //来源
    private lazy var sourceLabel: UILabel = UILabel(title: "来自： 小鱼儿", size: 12, color: UIColor.redColor())
    //微博内容
    private lazy var contentLabel: FFLabel = FFLabel(title: "爱是一道光，如此美妙！爱是一道光，如此美妙！爱是一道光，如此美妙！爱是一道光，如此美妙！", size: 14, color: UIColor.blackColor(), margin: statusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    //增加微博之间的分割视图
    private lazy var seperatorView: UIView = UIView()
    
}


extension StatusOriginalView {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        if text.hasPrefix("http") {
            let web = HomeWebViewController()
            web.urlString = text
            self.navController()?.pushViewController(web, animated: true)
        }
    }
}
